package com.zjzaki.thingsboard.dao;

import com.zjzaki.thingsboard.util.IPUtil;
import com.zjzaki.thingsboard.util.OkHttpUtils;

/**
 * @date 2022年09月25日 22:28:10
 */
public class ThingsBoardDao {

    static String IP = IPUtil.IP;

    /**
     * 登录方法
     *
     * @param username 用户名
     * @param password 密码
     * @return 示例 {"token":"","refreshToken":"","scope":null}
     */
    public static String login(String username, String password) {

        String sync = OkHttpUtils.builder().url("http://" + IP + ":8080/api/auth/login")
                //.addHeader("token", "xxxxxxx")
                .addParam("username", username)
                .addParam("password", password)
                .post(true) // true 为json提交方式
                .async();

        return sync;
    }

    /**
     * 刷新token值
     *
     * @param refreshToken 刷新token
     * @return 示例 {"token":"","refreshToken":"","scope":null}
     */
    public static String refreshToken(String refreshToken) {
        String sync = OkHttpUtils.builder().url("http://" + IP + ":8080/api/auth/token")
                //.addHeader("token", "xxxxxxx")
                .addParam("refreshToken", refreshToken)
                .post(true) // true 为json提交方式
                .async();

        return sync;
    }
    //http://iot.test.com/api/plugins/telemetry/DEVICE/e00c4cd0-a885-11e9-884f-57005a69df9c/keys/timeseries

    /**
     * 获取时间序列键
     *
     * @param entityType 实体类型
     * @param entityId   实体ID
     * @param token token
     * @return 示例 ["key1","key2"]
     */
    public static String getTimeseriesKeys(String entityType, String entityId, String token) {
        String url = "http://" + IP + ":8080/api/plugins/telemetry/" + entityType + "/" + entityId + "/keys/timeseries";

        String sync = OkHttpUtils.builder().url(url)
                .addHeader("X-Authorization:Bearer", token)
                //.addParam("name", "xxx").addParam("pass", "xxx")
                .get()
                .async();

        return sync;
    }

    /**
     * 得到最新遥测数据
     *
     * @param entityType 实体类型
     * @param entityId   实体ID
     * @param token
     * @return 示例
     */
    public static String getLatestTimeseries(String entityType, String entityId, String token) {
        String url = "http://" + IP + ":8080/api/plugins/telemetry/" + entityType + "/" + entityId + "/values/timeseries";
        String sync = OkHttpUtils.builder().url(url)
                .addHeader("X-Authorization:Bearer", token)
                //.addParam("name", "xxx").addParam("pass", "xxx")
                .get()
                .async();
        return sync;
    }

    /**
     * 上报数据
     *
     * @param entityType 实体类型
     * @param entityId   实体ID
     * @param key        键
     * @param value      值
     * @return
     */
    public static String saveEntityTelemetry(String entityType, String entityId, String token, String key, String value) {
        String url = "http://" + IP + ":8080/api/plugins/telemetry/" + entityType + "/" + entityId + "/timeseries/ANY";
        String sync = OkHttpUtils.builder().url(url)
                .addHeader("X-Authorization:Bearer", token)
                .addParam(key, value)
                .post(true) // true 为json提交方式
                .async();

        return sync;
    }

    /**
     * 上报数据，不登录系统
     *
     * @param access_token 许可token
     * @param key          键
     * @param value        值
     * @return 请求结果
     */
    public static String postEntityTelemetry(String access_token, String key, String value) {
        String url = "http://" + IP + ":8080/api/v1/" + access_token + "/telemetry";
        String sync = OkHttpUtils.builder().url(url)
                //.addHeader("X-Authorization:Bearer", token)
                .addParam(key, value)

                .post(true) // true 为json提交方式
                .async();

        return sync;
    }

    /**
     * 更新共享属性值，如果没有则添加一个
     *
     * @param token token
     * @param key 共享属性键
     * @param value 共享属性值
     * @param deviceId 设备ID
     * @return 请求的结果
     */
    public static String updateScope(String token, String key, String value, String deviceId) {
        String url = "http://" + IP + ":8080/api/plugins/telemetry/DEVICE/" + deviceId + "/SHARED_SCOPE";
        String sync = OkHttpUtils.builder().url(url)
                .addHeader("X-Authorization:Bearer", token)
                .addParam(key, value)
                .post(true) // true 为json提交方式
                .async();

        return sync;
    }

    /**
     * 得到共享属性
     *
     * @param token token
     * @param deviceId 设备ID
     * @return 得到的结果
     */
    public static String getScope(String token, String deviceId) {
        String url = "http://" + IP + ":8080/api/plugins/telemetry/DEVICE/" + deviceId + "/values/attributes/SHARED_SCOPE";
        String sync = OkHttpUtils.builder().url(url)
                .addHeader("X-Authorization:Bearer", token)
                .get()
                .async();

        return sync;
    }

    /**
     * 获取设备的服务端属性
     * @param token token
     * @param deviceId 设备ID
     * @return 请求到的结果
     */
    public static String getServerScope(String token,String deviceId){
        //http://43.139.18.244:8080/api/plugins/telemetry/DEVICE/77e4c260-bc11-11ed-b572-17f850f3a3a3/values/attributes/SERVER_SCOPE

        String url = "http://" + IP + ":8080/api/plugins/telemetry/DEVICE/" + deviceId + "/values/attributes/SERVER_SCOPE";
        String sync = OkHttpUtils.builder().url(url)
                .addHeader("X-Authorization:Bearer", token)
                .get()
                .async();

        return sync;
    }

    /**
     * 得到所有的设备
     *
     * @return 请求到的数据
     */
    public static String getAllDevice(String token) {
        //网络请求
        String url = "http://" + IP + ":8080/api/tenant/deviceInfos?pageSize=10&page=0&sortProperty=createdTime&sortOrder=ASC&deviceProfileId=&type=ddd";
        //http://43.139.18.244:8080/api/tenant/deviceInfos?pageSize=10&page=0&sortProperty=createdTime&sortOrder=DESC&deviceProfileId=
        //String url = "http://" + IP + ":8080/api/tenant/deviceInfos?pageSize=10&page=0&sortProperty=createdTime&sortOrder=DESC&deviceProfileId=";
        String devices = OkHttpUtils.builder().url(url)
                .addHeader("X-Authorization:Bearer", token)
                .get()
                .async();

        System.out.println(devices);
        return devices;
    }

    /**
     * 获取设备历史数据
     *
     * @param id       设备id
     * @param keys     获取的遥测key
     * @param startTs  Unix时间戳标识间隔的开始（以毫秒为单位）
     * @param endTs    Unix时间戳标识间隔的结束时间（以毫秒为单位）
     * @param interval 以毫秒为的聚合间隔单位 可为空
     * @param limit    要返回的最大数据点数或要处理的间隔
     * @param agg      聚合函数。MIN，MAX，AVG，SUM，COUNT，NONE之一
     * @return 请求到的数据
     */
    public static String getHistoricalData(String id, String keys, Long startTs, Long endTs, Integer interval, int limit, String agg, String token) {
        //http://43.139.18.244:8080/api/plugins/telemetry/DEVICE/216f8670-b2c2-11ed-ba42-b9132b4928cd/values/timeseries?keys=temp&startTs=1677030840000&endTs=1677117560000&interval=600000&limit=100&agg=AVG
        String url = "http://" + IP + ":8080/api/plugins/telemetry/DEVICE/" + id
                + "/values/timeseries?keys=" + keys + "&startTs=" + startTs
                + "&endTs=" + endTs + "&interval=" + interval + "&limit=" + limit + "&agg=" + agg;

        System.out.println("请求的地址-----》"+url);
        String devicesHistoricalData = OkHttpUtils.builder().url(url)
                .addHeader("X-Authorization:Bearer", token)
                .get()
                .async();

        return devicesHistoricalData;
    }
}
